import { get } from "../../utils/request";
Page({
  data: {
    bannerList: [],
    articles: [],
    page: 1, //要查询第几页的数据
    limit: 5, //每页的条数
    total: 0, //总条数
  },
  async getBannerList() {
    //调用远程接口返回轮播图数组
    const { bannerList } = await get("/bannerList");
    this.setData({ bannerList });
  },
  async getArticleList() {
    const { articles, total } = await get("/articlelist", {
      data: {
        page: this.data.page,
        limit: this.data.limit,
      },
    });
    this.setData({
      articles: [...this.data.articles, ...articles],
      total,
      page: this.data.page + 1,
    });
  },
  async onLoad(options) {
    //在页面加载的时候调用此方法
    this.getBannerList();
    this.getArticleList();
  },
  async onReachBottom() {
    //当滚动到底部的时候触发
    wx.showNavigationBarLoading(); //显示导航栏中的loading状态
    await this.getArticleList(); //再次调用getArticleList方法加载下一页
    wx.hideNavigationBarLoading(); //结束导航栏中的loading状态
  },
  async onPullDownRefresh() {
    //先清空数组，并重置页码
    this.data.articles = [];
    this.data.page = 1;
    //再次获取最新的第一页的数据
    await this.getArticleList();
    //停止下拉刷新动画
    wx.stopPullDownRefresh();
  },
  navigateToDetail(event) {
    //event.detail是出发事件时携带的参数  article
    wx.navigateTo({
      url: `/pages/article-detail/article-detail?id=${event.detail._id}`,
    });
  },
});
